<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function Star (id) {
            this.id = id;
            this.uname = 'aaa';
        }
        Star.prototype = {
            sayHi () {
                console.log(this);
            }
        }

        const instance = new Star(1);
        instance.sayHi();

        Star.prototype.sayHi();
        
        function fn (a, b, c) {
            console.log(a + b);
        }

        // fn.call()运行函数 + 改this + 传参
        fn.call(instance, 1, 2);

        // fn.apply()运行函数 + 改this + 传参
        fn.apply(instance, [1, 2, 3]);

        // 返回改this后的没有运行的函数
        const newFn = fn.bind(instance);
        // newFn(1, 2);
        const p = {
            sayHi () {
                console.log(this.name);
            }
        }

        const a = {
            name: 'zhangsan',
        }

        p.sayHi.call(a);
        p.sayHi.apply(a);
        const newFn1 = p.sayHi.bind(a);
        newFn1();
    </script>
</body>
</html>